<template>
  <el-dialog
    class="gl_dialog_TitleLine"
    width="780px"
    title="计算记录"
    :visible.sync="recordDialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-table v-loading="tableLoading" class="gl_mb_15 game_table_list" :data="tableData" stripe size="mini" style="width: 100%">
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column prop="creationTime" label="提交时间">
        <template slot-scope="{ row }">
          <span v-if="!row.creationTime || row.creationTime == '0001-01-01T00:00:00'">- -</span>
          <span v-else>{{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="completedTime" label="结束时间">
        <template slot-scope="{ row }">
          <span v-if="!row.completedTime || row.completedTime == '0001-01-01T00:00:00'">- -</span>
          <span v-else>{{ row.completedTime | FormatDate('yyyy-MM-dd HH:mm') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计算结果">
        <template slot-scope="{ row }">
          <el-button type="text" size="small" @click="openDetail(row)">详情</el-button>
          <el-button type="text" size="small" :loading="row.btnLoading" @click="reCalculation(row)">
            <span class="gl_color_warning">错误重算</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <calculationDetails ref="calculationDetails" />
  </el-dialog>
</template>
<script>
import calculationDetails from './calculationDetails';
import {
  api_GET_EventsManagementPointTask,
  api_GET_EventsManagementPointTaskReStartTaskGroupId
} from '@/extend/api/game.js';
export default {
  components: {
    calculationDetails
  },
  data() {
    return {
      tableLoading: false,
      tableData: [],
      recordDialogVisible: false,
      gameId: ''
    };
  },
  methods: {
    open(gameId) {
      this.gameId = gameId;
      this.getData();
      this.recordDialogVisible = true;
    },
    // 获取任务列表
    getData() {
      var that = this;
      that.tableLoading = true;
      const form = {
        competitionId: that.gameId
      };
      api_GET_EventsManagementPointTask(form).then(res => {
        that.tableData = res || [];
        if (that.tableData.length > 0) {
          that.tableData = that.tableData.map(item => {
            return { ...item, btnLoading: false };
          });
        }
        that.tableLoading = false;
      }, error => {
        that.tableLoading = false;
        const _msg = error.error.message || error.message;
        that.$message({ type: 'error', message: _msg || '获取数据失败！' });
      });
    },
    // 打开详情
    openDetail(row) {
      this.$refs.calculationDetails.open(row.id);
    },
    // 重新计算
    reCalculation(row) {
      var that = this;
      row.btnLoading = true;
      api_GET_EventsManagementPointTaskReStartTaskGroupId(row.id).then(res => {
        row.btnLoading = false;
        that.getData();
        that.$emit('refresh');
        that.$message({ type: 'success', message: '操作成功' });
      }).catch((error) => {
        row.btnLoading = false;
        const _msg = error.error.message || error.message;
        that.$message({ type: 'error', message: _msg || '操作失败！' });
      });
    }
  }
};
</script>

